package com.clkj.mobileinspector.utils;

import android.text.TextUtils;


import com.clkj.mobileinspector.model.Constant;
import com.clkj.mobileinspector.model.bean.OperationBean;
import com.clkj.mobileinspector.model.bean.QueryBean;
import com.clkj.mobileinspector.model.bean.QuerySuccessBean;
import com.lws.basic.utils.common.GsonUtils;
import com.lws.basic.utils.common.MyLogger;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 松
 * 2020/9/19 09:17
 */
public class JsonUtils {

    /**
     *  封装查询请求
     * @param keyValueList 请求参数
     * @param fields       返回字段
     * @param dataObjId    接口资源ID
     * @param orderByList  排序
     * @param page         分页
     * @param regionalismCode  接口所在区域
     * @return json
     */
    public static String  query(List<QueryBean.ParameterBean.ConditionBean.KeyValueListBean> keyValueList,
                              String fields,String dataObjId ,List<QueryBean.ParameterBean.OrderByBean> orderByList
                              ,QueryBean.ParameterBean.PageBean page , String regionalismCode)
    {
        QueryBean bean = new QueryBean();
        bean.setMessageId(RandomStringUtils.getRandomString(32));
        bean.setVersion(Constant.version);

        QueryBean.ParameterBean parameter = new QueryBean.ParameterBean();
        QueryBean.ParameterBean.ConditionBean condition = new QueryBean.ParameterBean.ConditionBean();
        condition.setKeyValueList(keyValueList);
        condition.setLogicalOperate("and");

        parameter.setPage(page);
        parameter.setOrderBy(orderByList);
        parameter.setNetworkCode(Constant.networkCode);
        parameter.setDataObjId(dataObjId);
        parameter.setFields(fields);
        parameter.setCondition(condition);
        parameter.setRegionalismCode(regionalismCode);
        bean.setParameter(parameter);
        return GsonUtils.getInstance().toJson(bean);

    }

    /**
     * 遍历字段值
     * @param list
     * @param filed
     * @return 值
     */
    public static String queryGetFiled(List<QuerySuccessBean.DataBean.DataListBean.FieldValuesBean> list, String filed ){
        for (QuerySuccessBean.DataBean.DataListBean.FieldValuesBean fieldValuesBean:list){
            if (filed.equals(fieldValuesBean.getField())){
                MyLogger.i(fieldValuesBean.getField()+":  "+ fieldValuesBean.getValue());
                if (TextUtils.isEmpty(fieldValuesBean.getValue())||fieldValuesBean.getValue().equals("null")){
                    return "";
                }else {
                    return fieldValuesBean.getValue();
                }

            }
        }
        return "";

    }


    /**
     * 封装 操作类接口
     * @param keyValueList  修改条件
     * @param operationType/1 新增，2 修改，3 删除
     * @param dataObjId   接口资源id
     * @param dataList    修改字段
     * @param regionalismCode  接口所在区域
     * @return json
     */
    public static String  operation(List<OperationBean.ParameterBean.OperationsBean.ConditionBean.KeyValueListBean> keyValueList
    ,int operationType,String dataObjId ,List<OperationBean.ParameterBean.OperationsBean.DataBean> dataList,String regionalismCode ){
        OperationBean operation= new OperationBean();
        operation.setMessageId(RandomStringUtils.getRandomString(32));
        operation.setVersion(Constant.version);
        OperationBean.ParameterBean parameterBean = new OperationBean.ParameterBean();
        parameterBean.setTransaction(1);
        List<OperationBean.ParameterBean.OperationsBean> operationsBeanList = new ArrayList<>();
        OperationBean.ParameterBean.OperationsBean operationsBean = new OperationBean.ParameterBean.OperationsBean();
        OperationBean.ParameterBean.OperationsBean.ConditionBean conditionBean = new OperationBean.ParameterBean.OperationsBean.ConditionBean();
        conditionBean.setLogicalOperate("and");
        conditionBean.setKeyValueList(keyValueList);
        operationsBean.setDataObjId(dataObjId);
        operationsBean.setOperationType(operationType);
        operationsBean.setNetworkCode(Constant.networkCode);
        operationsBean.setRegionalismCode(regionalismCode);
        operationsBean.setOperationId(RandomStringUtils.getRandomString(6));

        switch (operationType){
            case 1:
                operationsBean.setCondition(null);
                break;
            case 2:
                operationsBean.setCondition(conditionBean);
                break;
            case 3:
                break;
        }

        operationsBean.setData(dataList);
        operationsBeanList.add(operationsBean);
        parameterBean.setOperations(operationsBeanList);
        operation.setParameter(parameterBean);
        return GsonUtils.getInstance().toJson(operation);
    }
}
